package couk.mmtdigital.sm.view
{
	import couk.mmtdigital.sm.ApplicationFacade;
	import couk.mmtdigital.sm.model.FileListProxy;
	import couk.mmtdigital.sm.view.components.ButtonControlBar;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ButtonControlBarMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ButtonControlBarMediator";
		
		public function ButtonControlBarMediator(vc:Object=null)
		{
			super(NAME, vc);
		}
		
		private function onNewFolder(evt:Event):void{
			sendNotification(ApplicationFacade.FILEPANELNEWFOLDER);
		}
		
		private function onNewFile(evt:Event):void{
			sendNotification(ApplicationFacade.UPLOADVIEW);
		}
		
		private function onLogout(evt:Event):void{
			sendNotification(ApplicationFacade.LOGOUT);
		}
		
		override public function listNotificationInterests():Array{
			return [
				ApplicationFacade.LOGGED,
				ApplicationFacade.LOGGEDOUT,
				ApplicationFacade.UPDATELIST
			];
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case ApplicationFacade.LOGGED:
					buttonControlBar.currentState = "mainView";
					buttonControlBar.newFolder.addEventListener(MouseEvent.CLICK, onNewFolder);
					buttonControlBar.newFile.addEventListener(MouseEvent.CLICK, onNewFile);
					buttonControlBar.logout.addEventListener(MouseEvent.CLICK, onLogout);
					break;
				case ApplicationFacade.LOGGEDOUT:
					buttonControlBar.currentState = "login";
					break;
				case ApplicationFacade.UPDATELIST:
					var fileListProxy:FileListProxy = facade.retrieveProxy(FileListProxy.NAME) as FileListProxy;
					var a:Array = fileListProxy.currentFile.split("/");
					buttonControlBar.currentFolderLbl.text = "Current Dir: "+a[a.length-1];
			}
		}
		
		private function get buttonControlBar():ButtonControlBar{
			return viewComponent as ButtonControlBar;
		}
	}
}